home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / pdfdemo.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  13KB  |  555 lines

  1. /* pdfdemo.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  *
  4.  * Test bed and sample application for PDFlib
  5.  */
  6.  
  7. #ifdef DOS
  8. #include <process.h>
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13.  
  14. #include "pdf.h"
  15.  
  16. /* ------------------------------------------------------------- */
  17. static void
  18. gif_image(PDF *p)
  19. {
  20.     PDF_image    *image;
  21.  
  22. #define GIFFILE        "BINDINGS/C/acro_web.gif"
  23.  
  24.     if ((image = PDF_open_GIF(p, GIFFILE)) == NULL) {
  25.     fprintf(stderr, "Error: Couldn't analyze GIF image %s.\n", GIFFILE);
  26.     return;
  27.     }
  28.  
  29.     PDF_begin_page(p, image->width, image->height);
  30.     PDF_add_outline(p, "GIF image");
  31.  
  32.     PDF_place_image(p, image, 0.0, 0.0, 1.0);
  33.     PDF_close_GIF(p, image);
  34.  
  35.     PDF_end_page(p);
  36.  
  37. #undef GIFFILE
  38. }
  39.  
  40. /* ------------------------------------------------------------- */
  41. #ifdef USE_TIFF
  42. static void
  43. tiff_image(PDF *p)
  44. {
  45.     PDF_image    *image;
  46.  
  47. #define TIFFFILE    "BINDINGS/C/bible.tif"
  48.  
  49.     if ((image = PDF_open_TIFF(p, TIFFFILE)) == NULL) {
  50.     fprintf(stderr, "Error: Couldn't analyze TIFF image %s.\n", TIFFFILE);
  51.     return;
  52.     }
  53.  
  54.     PDF_begin_page(p, image->width, image->height);
  55.     PDF_add_outline(p, "TIFF image");
  56.  
  57.     PDF_place_image(p, image, 0.0, 0.0, 1.0);
  58.     PDF_close_TIFF(p, image);
  59.  
  60.     PDF_end_page(p);
  61.  
  62. #undef TIFFFILE
  63. }
  64. #endif
  65.  
  66. /* ------------------------------------------------------------- */
  67. static void
  68. raster_image_modified(PDF *p)
  69. {
  70.     PDF_image    *image;
  71.     float    scale;
  72.  
  73. #define JPEGFILE    "BINDINGS/C/nesrin.jpg"
  74.  
  75.     PDF_begin_page(p, a4.width, a4.height);
  76.     PDF_add_outline(p, "Transformed JPEG images");
  77.  
  78.     if ((image = PDF_open_JPEG(p, JPEGFILE)) == NULL) {
  79.     fprintf(stderr, "Error: Couldn't analyze JPEG image %s.\n", JPEGFILE);
  80.     return;
  81.     }
  82.     /* Put image data in PDF file and re-use with different transformations */
  83.     PDF_put_image(p, image);
  84.  
  85.     /* ----------------- first image ------------------- */
  86.     scale = a4.width/image->width;    /* fit image to page width */
  87.     PDF_execute_image(p, image, 0.0,  a4.height-image->height*scale, scale);
  88.  
  89.     /* ----------------- second image ------------------- */
  90.     scale = 0.5;
  91.     PDF_save(p);
  92.     PDF_rotate(p, 90.0);
  93.     PDF_execute_image(p, image, 0, -a4.width, scale);
  94.     PDF_restore(p);
  95.  
  96.     /* ----------------- third image ------------------- */
  97.     scale = 0.25;
  98.     PDF_save(p);
  99.     PDF_rotate(p, 45.0);
  100.     PDF_execute_image(p, image, 200, 0.0, scale);
  101.     PDF_restore(p);
  102.  
  103.     PDF_close_JPEG(p, image);
  104.     PDF_end_page(p);
  105.  
  106. #undef JPEGFILE
  107. }
  108.  
  109. /* ------------------------------------------------------------- */
  110. static void
  111. character_table(PDF *p)
  112. {
  113.     char    text[50];
  114.     int        i, j;
  115.     float    x, y;
  116.  
  117.     PDF_begin_page(p, a4.width, a4.height);
  118.     PDF_add_outline(p, "Character table");
  119.  
  120. #define LEFT        50
  121. #define TOP        700
  122. #define FONTSIZE    16
  123. #define FONTNAME    "Times-Roman"
  124.  
  125.     PDF_set_font(p, FONTNAME, 2*FONTSIZE, winansi);
  126.     PDF_show_xy(p, "ISOLatin1Encoding", LEFT, TOP+2*FONTSIZE);
  127.  
  128.     PDF_set_font(p, FONTNAME, FONTSIZE, winansi);
  129.     text[1] = 0;
  130.  
  131.     y = TOP;
  132.     for (i = 2; i < 16; i++) {
  133.     y -=  2*FONTSIZE;
  134.     x = LEFT;
  135.     for (j = 0; j < 16; j++) {
  136.         text[0] = i*16 + j;
  137.         PDF_show_xy(p, text, x, y);
  138.         x += 2*FONTSIZE;
  139.     }
  140.     }
  141.  
  142.     PDF_end_page(p);
  143.  
  144. #undef LEFT
  145. #undef TOP
  146. #undef FONTSIZE
  147. #undef FONTNAME
  148. }
  149.  
  150. /* ------------------------------------------------------------- */
  151. static void
  152. grid(PDF *p)
  153. {
  154. #define STEP        10
  155. #define FONTSIZE    10.0
  156. #define THICK        1.0
  157. #define THIN        0.01
  158.  
  159.     char buf[10];
  160.     float i, width = a4.width, height = a4.height;
  161.  
  162.     PDF_begin_page(p, width, height);
  163.     PDF_add_outline(p, "Grid");
  164.  
  165.     PDF_setlinewidth(p, THIN);
  166.     PDF_setdash(p, 1.0, 2.0);
  167.  
  168.     /* draw vertical lines */
  169.     for (i = 0; i < width; i += STEP) {
  170.     PDF_save(p);
  171.     if ((int) i % 100 == 0)
  172.         PDF_setlinewidth(p, THICK);
  173.     if ((int) i % 50 == 0)
  174.         PDF_setdash(p, 0.0, 0.0);
  175.     PDF_moveto(p, i, 0);
  176.     PDF_lineto(p, i, height);
  177.     PDF_stroke(p);
  178.     PDF_restore(p);
  179.     }
  180.  
  181.     /* draw horizontal lines */
  182.     for (i = 0; i < height; i += STEP) {
  183.     PDF_save(p);
  184.     if ((int) i % 50 == 0)
  185.         PDF_setdash(p, 0.0, 0.0);
  186.     if ((int) i % 100 == 0)
  187.         PDF_setlinewidth(p, THICK);
  188.     PDF_moveto(p, 0, i);
  189.     PDF_lineto(p, width, i);
  190.     PDF_stroke(p);
  191.     PDF_restore(p);
  192.     }
  193.  
  194. #define FONTNAME    "Helvetica-Bold"
  195. #define DELTA    9
  196. #define RADIUS    12.0
  197.  
  198.     PDF_set_font(p, FONTNAME, FONTSIZE, winansi);
  199.  
  200.     /* print captions */
  201.     for (i = 100; i < width; i+= 100) {
  202.     PDF_save(p);
  203.     PDF_setgray_fill(p, 1.0);
  204.     PDF_circle(p, i, 20.0, RADIUS);
  205.     PDF_fill(p);
  206.     PDF_restore(p);
  207.     sprintf(buf, "%d", (int) i);
  208.     PDF_show_xy(p, buf, i - DELTA, 20.0 - FONTSIZE/3);
  209.     }
  210.  
  211.     for (i = 100; i < height; i+= 100) {
  212.     PDF_save(p);
  213.     PDF_setgray_fill(p, 1.0);
  214.     PDF_circle(p, 40.0, i, RADIUS);
  215.     PDF_fill(p);
  216.     PDF_restore(p);
  217.     sprintf(buf, "%d", (int) i);
  218.     PDF_show_xy(p, buf, 40.0 - DELTA, i - FONTSIZE/3);
  219.     }
  220.  
  221.     PDF_end_page(p);
  222.  
  223. #undef STEP
  224. #undef FONTSIZE
  225. #undef DELTA
  226. #undef RADIUS
  227. #undef FONTNAME
  228. }
  229.  
  230. /* ------------------------------------------------------------- */
  231. static void
  232. shaded_circle(PDF *p)
  233. {
  234.     int i;
  235.  
  236.     PDF_begin_page(p, a4.width, a4.height);
  237.     PDF_add_outline(p, "Shaded circle");
  238.  
  239.     for (i = 255; i >= 0; i -= 3) {
  240.     PDF_setrgbcolor_fill(p, 1.0 - (float) i/255, 1.0 - (float) i/255, 1.0);
  241.     PDF_circle(p, 300, 400, i);
  242.     PDF_fill(p);
  243.     }
  244.     PDF_end_page(p);
  245. }
  246.  
  247.  
  248. /* ------------------------------------------------------------- */
  249. static void
  250. shaded_circle_linear(PDF *p)
  251. {
  252.     int i, step = 1;
  253.     float gray = 0.1, r;
  254.  
  255.     r = pow(1.0/gray, 1.0/255.0); /* generate perceptual linear color blend */
  256.     r = pow(r, step);          /* skip steps to improve performance */
  257.  
  258.     PDF_begin_page(p, a4.width, a4.height);
  259.     PDF_add_outline(p, "Shaded circle (linearized)");
  260.  
  261.     for (i = 255; i >= 0; i -= step) {
  262.     PDF_setrgbcolor_fill(p, gray, gray, 1.0);
  263.     PDF_circle(p, 300, 400, i);
  264.     PDF_fill(p);
  265.     gray *= r;
  266.     }
  267.     PDF_end_page(p);
  268. }
  269.  
  270. /* ------------------------------------------------------------- */
  271. static void
  272. centered_text(PDF *p)
  273. {
  274.     float    y, width;
  275.     char    **cp;
  276.     char    *text[] = {
  277.             "Hat der alte Hexenmeister",
  278.             "Sich doch einmal wegbegeben!",
  279.             "Und nun sollen seine Geister",
  280.             "Auch nach meinem Willen leben.",
  281.             "Seine Wort' und Werke",
  282.             "Merkt ich und den Brauch,",
  283.             "Und mit Geistesst\344rke",
  284.             "Tu ich Wunder auch.",
  285.             "Walle! walle",
  286.             "Manche Strecke,",
  287.             "Da\337, zum Zwecke,",
  288.             "Wasser flie\337e",
  289.             "Und mit reichem, vollem Schwalle",
  290.             "Zu dem Bade sich ergie\337e.",
  291.             NULL };
  292.  
  293. #define FONTSIZE    24
  294. #define FONTNAME    "Times-Roman"
  295. #define CENTER        300
  296. #define TOP        750
  297.  
  298.     PDF_begin_page(p, a4.width, a4.height);
  299.     PDF_add_outline(p, "Centered text");
  300.  
  301.     PDF_set_font(p, FONTNAME, FONTSIZE, winansi);
  302.  
  303.     y = TOP;
  304.  
  305.     for (cp = text; *cp; cp++) {
  306.     width = PDF_stringwidth(p, (unsigned char *) *cp);
  307.     PDF_show_xy(p, *cp, CENTER - width/2, y);
  308.     y -= 1.5 * FONTSIZE;
  309.     }
  310.  
  311.     PDF_end_page(p);
  312.  
  313. #undef FONTSIZE
  314. #undef FONTNAME
  315. #undef RIGHT
  316. #undef TOP
  317. }
  318.  
  319. /* ------------------------------------------------------------- */
  320. static void
  321. inline_image(PDF *p)
  322. {
  323.     byte        *buf, *bp;
  324.     long        buflen;
  325.     PDF_image        image;
  326.     int            i, j;
  327.     float        y, sx, sy;
  328.  
  329. #define LEFT        50.0
  330.  
  331.     PDF_begin_page(p, 900.0, 600.0);
  332.     PDF_add_outline(p, "Inline image");
  333.  
  334.     /* 
  335.      * Since the image interface doesn't support non-proportional
  336.      * scaling, we will use PDF_scale() instead to stretch the image.
  337.      */
  338.     image.width        = 256;
  339.     image.height    = 1;
  340.     image.bpc        = 8;
  341.     image.components    = 3;
  342.     image.colorspace    = DeviceRGB;
  343.  
  344.     sx = 3.0;    /* desired horizontal scaling factor */
  345.     sy = 128;    /* desired height of one color band */
  346.  
  347.     buflen = image.width * image.height * image.components;
  348.     buf = PDF_malloc(buflen, "inline_image");
  349.  
  350.     if (buf == NULL) {
  351.     fprintf(stderr, "Not enough memory for inline image!\n");
  352.     return;
  353.     }
  354.  
  355.     /* 
  356.      * In positioning the images below, we will have to compensate 
  357.      * for the scaling.
  358.      */
  359.     PDF_scale(p, sx, sy);        /* stretch image */
  360.  
  361.     /* now fill the buffer with fake image data (simple color ramp) */
  362.     for (bp = buf, i=0; i<image.height; i++) {
  363.     for (j=0; j<image.width; j++) {
  364.         *bp++ = (byte) (j % 256);    /* red blend */
  365.         *bp++ = 0;
  366.         *bp++ = 0;
  367.     }
  368.     }
  369.  
  370.     y = LEFT;
  371.     PDF_data_source_from_buf(p, &image.src, buf, buflen);
  372.     PDF_place_inline_image(p, &image, LEFT/sx, y/sy, 1.0);
  373.  
  374.     for (bp = buf, i=0; i<image.height; i++) {
  375.     for (j=0; j<image.width; j++) {
  376.         *bp++ = 0;
  377.         *bp++ = (byte) (j % 256);    /* green blend */
  378.         *bp++ = 0;
  379.     }
  380.     }
  381.  
  382.     y += image.height * sy;        /* position the image */
  383.     PDF_data_source_from_buf(p, &image.src, buf, buflen);
  384.     PDF_place_inline_image(p, &image, LEFT/sx, y/sy, 1.0);
  385.  
  386.     for (bp = buf, i=0; i<image.height; i++) {
  387.     for (j=0; j<image.width; j++) {
  388.         *bp++ = 0;
  389.         *bp++ = 0;
  390.         *bp++ = (byte) (j % 256);    /* blue blend */
  391.     }
  392.     }
  393.  
  394.     y += image.height * sy;        /* position the image */
  395.     PDF_data_source_from_buf(p, &image.src, buf, buflen);
  396.     PDF_place_inline_image(p, &image, LEFT/sx, y/sy, 1.0);
  397.  
  398.     image.components    = 1;        /* now a single component image */
  399.     image.colorspace    = DeviceGray;
  400.  
  401.     for (bp = buf, i=0; i<image.height; i++) {
  402.     for (j=0; j<image.width; j++) {
  403.         *bp++ = (byte) (j % 256);    /* gray blend */
  404.     }
  405.     }
  406.  
  407.     y += image.height * sy;        /* position the image */
  408.     buflen = image.width * image.height * image.components;
  409.     PDF_data_source_from_buf(p, &image.src, buf, buflen);
  410.     PDF_place_inline_image(p, &image, LEFT/sx, y/sy, 1.0);
  411.  
  412.     PDF_free(buf);
  413.     PDF_end_page(p);
  414. #undef LEFT
  415. }
  416.  
  417. /* ------------------------------------------------------------- */
  418. static void
  419. radial_structure(PDF *p)
  420. {
  421.     float alpha;
  422.  
  423.     PDF_begin_page(p, a4.width, a4.height);
  424.     PDF_add_outline(p, "Radial structure");
  425.  
  426.     PDF_translate(p, 300, 400);
  427.     PDF_setlinewidth(p, 0.1);
  428.  
  429. #ifdef NOT_EXACT
  430.     /* 
  431.      * Mathematically, the following are equivalent. However, due
  432.      * to massive accumulation of rounding errors the first variant
  433.      * produces visible inaccuracy artifacts (try it!)
  434.      */
  435.     for (alpha = 0; alpha < 360; alpha++) {
  436.     PDF_moveto(p, 0.0, 0.0);
  437.     PDF_lineto(p, 250.0, 0.0);
  438.     PDF_stroke(p);
  439.     PDF_rotate(p, 1.0);
  440.     }
  441. #endif
  442.  
  443.     /* better solution: don't accumulate rounding errors */
  444.     for (alpha = 0; alpha < 360; alpha++) {
  445.     PDF_save(p);
  446.     PDF_rotate(p, alpha);
  447.     PDF_moveto(p, 0.0, 0.0);
  448.     PDF_lineto(p, 250.0, 0.0);
  449.     PDF_stroke(p);
  450.     PDF_restore(p);
  451.     }
  452.  
  453.     PDF_end_page(p);
  454. }
  455.  
  456. /* ------------------------------------------------------------- */
  457. static void
  458. random_data_graph(PDF *p)
  459. {
  460.     float x;
  461.  
  462.     PDF_begin_page(p, a4.width, a4.height);
  463.     PDF_add_outline(p, "Random graph");
  464.  
  465. #define STEP    10.0
  466. #define MARGIN    50.0
  467. #define RIGHT    500.0
  468. #define TOP    800.0
  469.     PDF_setlinewidth(p, 2);
  470.     PDF_moveto(p, RIGHT, MARGIN);
  471.     PDF_lineto(p, MARGIN, MARGIN);
  472.     PDF_lineto(p, MARGIN, TOP);
  473.     PDF_setgray_stroke(p, 0);        /* black */
  474.     PDF_stroke(p);
  475.  
  476.     PDF_setlinewidth(p, 1);
  477.     PDF_moveto(p, MARGIN, MARGIN);
  478.  
  479.     /* construct some random graph data */
  480.     PDF_setrgbcolor_stroke(p, 1.0, 0, 0);    /* red */
  481.     for (x=MARGIN; x<RIGHT; x+=STEP)
  482.     PDF_lineto(p, x, x + (TOP-MARGIN)/2.0*rand()/(RAND_MAX+1.0));
  483.  
  484.     PDF_stroke(p);
  485.  
  486.     PDF_setrgbcolor_stroke(p, 0, 1.0, 0);    /* green */
  487.     PDF_moveto(p, MARGIN, MARGIN);
  488.     for (x=MARGIN; x<RIGHT; x+=STEP)
  489.     PDF_lineto(p, x, 
  490.         MARGIN + TOP*(x-MARGIN)*(x-MARGIN)/((RIGHT-MARGIN)*(RIGHT-MARGIN)));
  491.  
  492.     PDF_stroke(p);
  493.  
  494.     PDF_setrgbcolor_stroke(p, 0, 0, 1.0);    /* blue */
  495.     PDF_moveto(p, MARGIN, MARGIN);
  496.     for (x=MARGIN; x<RIGHT; x+=STEP)
  497.     PDF_lineto(p, x, MARGIN + x + MARGIN*rand()/(RAND_MAX+1.0));
  498.  
  499.     PDF_stroke(p);
  500.  
  501.     PDF_end_page(p);
  502.  
  503. #undef STEP
  504. #undef MARGIN
  505. #undef RIGHT
  506. #undef TOP
  507. }
  508.  
  509. void
  510. main(int argc, char *argv[])
  511. {
  512.     char    *filename;
  513.     FILE    *pdffile;
  514.     PDF_info    *info;
  515.     PDF        *p;
  516.  
  517.     if (argc < 2)
  518.     filename = "demo.pdf";
  519.     else
  520.     filename = argv[1];
  521.  
  522.     if ((pdffile = fopen(filename, WRITEMODE)) == NULL) {
  523.         fprintf(stdout, "Couldn't open PDF file '%s'!\n", filename);
  524.     exit(1);
  525.     }
  526.  
  527.     info = PDF_get_info();
  528.     info->Creator    = "PDFdemo";
  529.     info->Title        = "PDFlib demo application";
  530.     info->Subject    = "Check many PDFlib function calls";
  531.     info->Author    = "Thomas Merz";
  532.     info->binary_mode   = false;
  533.  
  534.     p = PDF_open(pdffile, info);
  535.  
  536. #ifdef USE_TIFF
  537.     tiff_image(p);
  538. #endif
  539.  
  540.     gif_image(p);
  541.     raster_image_modified(p);
  542.     inline_image(p);
  543.     centered_text(p);
  544.     character_table(p);
  545.     grid(p);
  546.     shaded_circle(p);
  547.     shaded_circle_linear(p);
  548.     radial_structure(p);
  549.     random_data_graph(p);
  550.  
  551.     PDF_close(p);
  552.  
  553.     exit(0);
  554. }
  555.